<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="${contextPath}/staticwx/style/weui.css"/>
    <link rel="stylesheet" href="${contextPath}/staticwx/style/weui2.css"/>
    <link rel="stylesheet" href="${contextPath}/staticwx/style/weui3.css"/>
    <script src="${contextPath}/staticwx/zepto.min.js"></script>
    <script>
        $(function(){
            $('#formSubmitBtn').click(function(){
                var amount=$("input[name='amount']:checked").val();
                $("#totalAmount").val(amount);
                subOrder();
            });

        });

    </script>
</head>

<body ontouchstart style="background-color: #f8f8f8;">
<div class="page-bd">
    <div class="weui-form-preview">
        <div class="weui-form-preview-hd">
            <label class="weui-form-preview-label">充值账户</label>
            <em class="weui-form-preview-value">${member.tel!}</em>
        </div>
        <div class="weui-form-preview-bd">
            <p>
                <label class="weui-form-preview-label">当前余额</label>
                <span class="weui-form-preview-value">￥${account.balance?string("0.00")}</span>
            </p>
        </div>

    </div>
    <div class="weui_cells_title">请选择充值金额</div>
    <div class="weui_cells weui_cells_radio">
        <label class="weui_cell weui_check_label" for="x11">
            <div class="weui_cell_bd weui_cell_primary">
                <p>100元</p>
            </div>
            <div class="weui_cell_ft">
                <input type="radio" class="weui_check" name="amount" id="x11"  checked="checked" value="100">
                <span class="weui_icon_checked"></span>
            </div>
        </label>
        <label class="weui_cell weui_check_label" for="x12">
            <div class="weui_cell_bd weui_cell_primary">
                <p>200元</p>
            </div>
            <div class="weui_cell_ft">
                <input type="radio" name="amount" class="weui_check" id="x12" value="200">
                <span class="weui_icon_checked"></span>
            </div>
        </label>
        <label class="weui_cell weui_check_label" for="x13">
        <div class="weui_cell_bd weui_cell_primary">
            <p>300元</p>
        </div>
        <div class="weui_cell_ft">
            <input type="radio" name="amount" class="weui_check" id="x13" value="300">
            <span class="weui_icon_checked"></span>
        </div>
        </label>
        <label class="weui_cell weui_check_label" for="x14">
            <div class="weui_cell_bd weui_cell_primary">
                <p>500元</p>
            </div>
            <div class="weui_cell_ft">
                <input type="radio" name="amount" class="weui_check" id="x14" value="500">
                <span class="weui_icon_checked"></span>
            </div>
        </label>
        <label class="weui_cell weui_check_label" for="x15">
            <div class="weui_cell_bd weui_cell_primary">
                <p>1000元</p>
            </div>
            <div class="weui_cell_ft">
                <input type="radio" name="amount" class="weui_check" id="x15" value="1000">
                <span class="weui_icon_checked"></span>
            </div>
        </label>
    </div>
</div>

<div class="weui_btn_area">
    <input type="hidden" name="mid" id="memberId" value="${mid!}"/>
    <input type="hidden" name="orderNo" id="orderNo" value=""/>

    <input type="hidden" id="appId" value=""/>
    <input type="hidden" id="timeStamp" value=""/>
    <input type="hidden" id="nonceStr" value=""/>
    <input type="hidden" id="packages" value=""/>
    <input type="hidden" id="paySign" value=""/>

    <input type="hidden" id="payType" value="1"/>
    <input type="hidden" id="orderType" value="2"/>
    <input type="hidden" id="totalAmount" value=""/>
    <input type="hidden" name="openId" id="openId" value="${Session['cur_openid']!}"/>
    <input type="hidden" name="cityId" id="cityId" value="${Session['cur_city_id']!}"/>
    <a id="formSubmitBtn" href="javascript:" class="weui_btn weui_btn_primary">立即充值</a>
</div>
</body>
<script src="${contextPath}/staticwx/jweixin-1.0.0.js" type="text/javascript"></script>
<script type="text/javascript">
    /*提交订单数据*/
    function  subOrder() {
        var orderNo=$("#orderNo").val();
        var payType=$("#payType").val();
        if(orderNo){//表示订单已生成
            if(payType==1){
                var openId=$("#openId").val();
                var appId=$("#appId").val();
                var timeStamp=$("#timeStamp").val();
                var nonceStr=$("#nonceStr").val();
                var packages=$("#packages").val();
                var paySign=$("#paySign").val();
                callWxpay(appId,timeStamp,nonceStr,packages,paySign,orderNo);
                return false;
            }
        }else{
            var orderType=$("#orderType").val();//1-常规下单；2-充值订单;3-收款订单;
            var cityId=$("#cityId").val();
            var totalAmount=$("#totalAmount").val();
            var memberId=$("#memberId").val();
            createOrder(orderType,payType,cityId,totalAmount,memberId);
        }

    }

    /*创建订单*/
    function createOrder(orderType,payType,cityId,totalAmount,memberId){
        $.ajax({
            url : '${contextPath}/wx/order/createOrder.htm',
            data : {
                totalAmount:$("#totalAmount").val(),
                memberId:$("#memberId").val(),
                orderType:$("#orderType").val(),
                payType:$("#payType").val(),
                cityId:$("#cityId").val()
            },
            type : 'post',
            cache : false,
            dataType : 'json',
            success : function(data) {
                var json = $.parseJSON(data);
                if (json.isSuccess == true) {
                    var orderNo=json.orderNo;
                    var openId=$("#openId").val();
                    $("#orderNo").val(orderNo);
                    if(payType==1){
                        doWxPay(orderNo,openId);
                    }
                }else{
                    alert(json.errMessage);
                    //showMsg(json.errMessage);
                }
            },
            error : function() {
                //alert("订单提交异常！");
            }
        });
    }

    /*获取微信支付设置*/
    function doWxPay(orderNo,openId){
        $.ajax({
            url : '${contextPath}/wx/wxpay/fastPay.htm',
            data : {orderNo:orderNo,openId:openId},
            type : 'post',
            cache : false,
            dataType : 'json',
            success : function(data) {
                var json = $.parseJSON(data);
                if (json.isSuccess == true) {
                    var appId=json.appId;
                    var timeStamp=json.timeStamp;
                    var nonceStr=json.nonceStr;
                    var packages=json.packages;
                    var paySign=json.sign;
                    $("#appId").val(appId);
                    $("#timeStamp").val(timeStamp);
                    $("#nonceStr").val(nonceStr);
                    $("#packages").val(packages);
                    $("#paySign").val(paySign);
                    callWxpay(appId,timeStamp,nonceStr,packages,paySign,orderNo);
                }else{
                    showMsg(data.msg);
                }
            },
            error : function() {
                //alert("订单提交异常！");
            }
        });

    }
    /*发起微信支付*/
    function callWxpay(appId,timeStamp,nonceStr,packages,sign,orderNo){
        var appId=appId;
        var timeStamp=timeStamp;
        var nonceStr =nonceStr;
        var packages=packages;
        var paySign=sign;
        WeixinJSBridge.invoke('getBrandWCPayRequest',{
            "appId" : appId,"timeStamp" : timeStamp, "nonceStr" : nonceStr, "package" : packages,"signType" : "MD5", "paySign" : paySign
        },function(res){
            WeixinJSBridge.log(res.err_msg);
            if(res.err_msg == "get_brand_wcpay_request:ok"){
                location.href="${contextPath}/wx/wxpay/result.htm?oid="+orderNo+"&result=SUCCESS";
            }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                alert("支付取消!");
            }else{
                alert(res.err_msg);
                alert("支付失败!");
            }
        })
    }
</script>
</html>